/**
 * Created by dale on 2017/8/24.
 */
$(document).ready(function () {


    var form = new xhForm($('#xh_form'));
    form.parse();
    $('.tab-bar li a').each(function () {
        $(this).removeClass('btn').removeClass('btn-default');
    });
    $(".form-date").datetimepicker({format: 'yyyy-mm-dd', language: 'zh-CN', minView: "month", autoclose: true});

    var id=addressUri.getParam('iBPPId');
    var name=addressUri.getParam('name');
    var type=addressUri.getParam('type');
    if(type!=1) {
        $('[name=iMarketId]').select2();
        $('[name=iOperateId]').select2();
        $('[name="sYear[]"]').select2();
    }
    $('[name="iBPPId"]').val(id);
    $('[name="sSchoolName"]').val(unescape(name));

    //市场专员选中事件
    $('[name=iMarketId]').on('change',function () {
        setPhone('sMarketPhone',$('[name=iMarketId]').val());
    });

    
    $('#btn_copy').click(function () {
        var Url2=document.getElementById("readUrl");
        Url2.select(); // 选择对象
        document.execCommand("Copy"); // 执行浏览器复制命令
        window.top.swal('成功','连接已成功复制','success');
    });

    if(transData!='1'){
        if(type==1){
            $('title')[0].innerHTML=transData['sSchoolName'];
            $.each(transData, function(key, value){
                var _dom = $('#xh_form').find('[name$='+ key +']');
                if(!_dom) return true;
                _dom.html(value);
            });
            var planList= $('#planList');

            //设置禁用

            planList.treegrid({
                onSelect:function (row) {
                    keyId=row['keyId'];
                    planList.treegrid('expand',keyId);

                },
                onBeforeSelect:function (row) {
                    if(keyId==undefined){
                        return true;
                    }else {
                        if(keyId==row['keyId']){
                            planList.treegrid('unselect',keyId);
                            planList.treegrid('collapse',keyId);
                            keyId=null;
                            return false;
                        }
                    }
                },
                onBeforeCheck:function (row) {
                    if(keyId==undefined){
                        return true;
                    }else {
                        if(keyId==row['keyId']){
                            planList.treegrid('uncheckRow',keyId);
                            keyId=null;
                            return false;
                        }
                    }
                }

            });
        }else {
            form.fill(transData);
            $('[name=iMarketId]').trigger('change');
            $('[name=iOperateId]').trigger('change');
            if(transData['sYear'] != null && transData['sYear'] !=''){
                $('[name="sYear[]"]').val(transData['sYear'].split(',')).trigger('change');
            }
            for(var i=0;i<=2;i++){
                $($('[name="sGrade[]"]')[i]).val(transData['sGrade'][i]);
                $($('[name="iServiceCost[]"]')[i]).val(transData['iServiceCost'][i]);
                $($('[name="iTabletCost[]"]')[i]).val(transData['iTabletCost'][i]);
                $($('[name="sContentDate[]"]')[i]).val(transData['sContentDate'][i]);
                $($('[name="sPayDate[]"]')[i]).val(transData['sPayDate'][i]);

                $($('[name="tabletVersion[]"]')[i]).val(transData['tabletVersion'][i]);
                $($('[name="iChargeMode[]"]')[i]).val(transData['iChargeMode'][i]);
                $($('[name="tPayedDate[]"]')[i]).val(transData['tPayedDate'][i]);
            }
        }
    }
    
    // $('[name$="tStartTime"]').val(new Date().format('yyyy-MM-dd'));
    //
    $('#bt_modal_confirm',parent.document).click(function () {
        $('#bt_modal_confirm',parent.document).addClass('disabled');
        xhExtra.setBtnLoading($('#bt_modal_confirm',parent.document));
        $.ajax({
            url:modelUrl+'plan/saveTrans',
            data:$('#xh_form').serialize(),
            type:'POST',
            success:function (data) {
                // window.top.xhTop.createNewTab('项目计划流程列表','bpm/plan/planList',true);
                window.top.xhTop.getActiveFrame().planList.treegrid('reload');
               window.top.xhTop.closeModal();
            },
            error:function (data) {
                console.log(data);
            }
        });
    });

    if(addressUri.getParam('type')!=1){
        modalBind.bindClick();
    }

    $('#doPrint', window.top.document).click(function(){
        print();
    });

});


// function checkInfo() {
//     //基本信息页验证
//     var bVerify = $('[name=bVerify]').val();
//     var tVerifyDate = $('[name=tVerifyDate]').val();
//     var iStudentNum = $('[name=iStudentNum]').val();
//     var iTeacherNum = $('[name=iTeacherNum]').val();
//     var sYear = $('[name=sYear[]]').val();
//     var sClasses = $('[name=sClasses]').val();
//
//     //培训页验证
//     var tParentTrain = $('[name=tParentTrain]').val();
//     var tStuTrain = $('[name=tStuTrain]').val();
//     var tTechInTrain = $('[name=tTechInTrain]').val();
//     var tTechOpTrain = $('[name=tTechOpTrain]').val();
//
//
//
//
// }

function setPhone(_name,userId) {
    $.ajax({
        url:modelUrl+'plan/getPhone/'+userId,
        data:{},
        dataType:'json',
        method:'post',
        success:function (data) {
            $('[name='+_name+']').val(data.phone);
        },
        error:function (data) {

        }
    });
}
